﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class ProductListControl : YuanlinUserControl,
        IReplyConverter<ProductReply, GetListOperation>
    {
        protected ValuedDropDownList productCategoryDropDownList;
        protected ValuedTextBox searchTextBox;
        protected ResourceButton SearchButton;
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;
        private EntityList products;
        private int productCategoryId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.ProductCategoryId);
        private string queryText = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.QueryText);
        public int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            if (!Page.IsPostBack && !AjaxManager.IsCallBack)
            {
                ProductListRequest request = new ProductListRequest();
                request.CategoryId = productCategoryId;
                request.Title = queryText;
                request.PageIndex = pager.PageIndex;
                request.PageSize = pager.PageSize;
                requestBinders.Add(RequestHelper.BuildGetProductListRequest(this, request));
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            productCategoryDropDownList.SelectedIndexChanged += new EventHandler(productCategoryDropDownList_SelectedIndexChanged);
            SearchButton.Click += new EventHandler(SearchButton_Click);
            AjaxManager.Register(this, "AjaxMethod");
        }
        private void BindData()
        {
            #region 绑定下拉控件

            ProductCategory topCategory = new ProductCategory();
            topCategory.Title.Value = "<选择植物分类>";
            topCategory.EntityId.Value = -1;

            ProductCategoryListRequest request = new ProductCategoryListRequest();
            request.CategoryType = 0;
            EntityList topCategories = BusinessManager.GetProductCategoryList(RequestHelper.BuildGetProductCategoryListRequest(request));

            topCategories.Insert(0, topCategory);
            productCategoryDropDownList.DataSource = topCategories;
            productCategoryDropDownList.DataValueField = "EntityId";
            productCategoryDropDownList.DataTextField = "Title";
            productCategoryDropDownList.DataBind();
            productCategoryDropDownList.Value = productCategoryId.ToString();

            #endregion

            if (list == null)
            {
                return;
            }
            list.DataSource = products;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(products.TotalCount);
                currentPage.TotalRecords = (products.TotalCount);
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }
        private void SearchButton_Click(object sender, EventArgs e)
        {
            Context.Response.Redirect(SiteUrls.Instance().GetProductListUrl("", System.Web.HttpUtility.UrlEncode(this.searchTextBox.Text.Trim())));
        }
        private void productCategoryDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Context.Response.Redirect(SiteUrls.Instance().GetProductListUrl(this.productCategoryDropDownList.Value, string.Empty));
        }

        #region Ajax Method

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public void DeleteProduct(int productId)
        {
            BusinessManager.DeleteProduct(productId);
        }

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public int DeleteProducts(string entityIds)
        {
            if (string.IsNullOrEmpty(entityIds))
            {
                return 0;
            }
            string[] entityIdArray = entityIds.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            int entityId = 0;
            foreach (string stringEntityId in entityIdArray)
            {
                try
                {
                    entityId = Int32.Parse(stringEntityId);
                }
                catch
                {
                    continue;
                }
                if (entityId > 0)
                {
                    DeleteProduct(entityId);
                }
            }
            return 0;
        }

        #endregion

        #region IReplyConverter<ProductReply,GetListOperation> Members

        public void ConvertReply(ProductReply reply, GetListOperation operation)
        {
            if (reply != null)
            {

                products = reply.EntityList;

                if (!Page.IsPostBack && !AjaxManager.IsCallBack)
                {
                    BindData();
                }
            }
        }

        #endregion
    }
}
